#!/usr/bin/perl

package Swignition::Export::Recipe;
use Swignition::Export;
use Swignition::Export::Contact;
use HTML::Entities qw(encode_entities_numeric);
use MIME::Base64;
use POSIX;

sub is
{
	my $subject = shift;
	return unless ($subject);
	return unless (ref $subject eq 'Swignition::RDFModel::Subject');	
	return
		unless (defined $subject->property('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'));
	foreach my $Type ( @{$subject
		->property('http://www.w3.org/1999/02/22-rdf-syntax-ns#type')
		->valObjects} )
	{
		my $type = $Type->toString;
		return 1
			if ($type eq 'http://donnafales.com/2002/07/28/recipe-schema#Recipe');
	}
	return;
}

sub outputs
{
	return [
		{
			code  => 'RecipeBook',
			name  => 'RecipeBook XML',
			type  => 'text/xml',
			multi => 1
		}
	];
}

sub outRecipeBook
{
	return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
		. "<!DOCTYPE recipe PUBLIC \"-//Happy-Monkey//DTD RecipeBook//EN\" "
		. "\"http://www.happy-monkey.net/recipebook/recipebook.dtd\">\n"
		. _outRecipeBook(@_);
}

sub multiRecipeBook
{
	my $triples = shift;
	my $rv = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
		. "<!DOCTYPE cookbook PUBLIC \"-//Happy-Monkey//DTD RecipeBook//EN\" "
		. "\"http://www.happy-monkey.net/recipebook/recipebook.dtd\">\n"
		. "<cookbook>\n\n";
	my $rv2;
	my $page;

	foreach my $subject (values %$triples)
	{
		$rv2 .= _outRecipeBook($triples, $subject->uri)
			if ( is($subject) );
		$page = $subject->{page}
			if ($subject->{page});
	}
	
	my $S = findSubject($triples, $page->uri);
	my $title = firstOfLiteral($S,
		['http://purl.org/dc/terms/title',
		'http://purl.org/dc/elements/1.1/title'],
		['http://www.w3.org/1999/02/22-rdf-syntax-ns#value']);
	$rv .= sprintf("<title>%s</title>\n\n",
		encode_entities_numeric($title));
		
	$rv .= "$rv2</cookbook>\n\n";
	return $rv;
}

sub _outRecipeBook
{
	my $triples = shift;
	my $subject = shift;
	my $rv = "<recipe>\n";
	
	my $S = findSubject($triples, $subject);

	my $title = firstOfLiteral($S,
		['http://purl.org/dc/terms/title',
		'http://purl.org/dc/elements/1.1/title'],
		['http://www.w3.org/1999/02/22-rdf-syntax-ns#value']);
	$rv .= sprintf("\t<title>%s</title>\n",
		encode_entities_numeric($title));

	$rv .= "\t<recipeinfo>\n";

	my @creators = allOfObj($S,
		'http://purl.org/dc/terms/creator',
		'http://purl.org/dc/elements/1.1/creator',
		'urn:ietf:rfc:4287#author',
		'http://xmlns.com/foaf/0.1/maker',
		'urn:ietf:rfc:2445#organizer',
		'http://www.w3.org/2002/12/cal/ical#organizer');
	foreach my $creator (@creators)
	{
		if ($creator->type)
		{
			my $C = findSubject($triples, $creator->toString);
			if ($C && Swignition::Export::Contact::is($C))
			{
				$rv .= sprintf("\t\t<author>%s</author>\n",
					encode_entities_numeric(Swignition::Export::Contact::outName($triples, $C->uri)));
				next;
			}
		}
		$rv .= sprintf("\t\t<author>%s</author>\n",
			encode_entities_numeric($creator->toString));
	}

	my $notes = firstOfLiteral($S,
		['http://purl.org/dc/terms/abstract',
		'http://purl.org/dc/terms/description',
		'http://purl.org/dc/elements/1.1/description'],
		['http://www.w3.org/1999/02/22-rdf-syntax-ns#value']);
	$rv .= sprintf("\t\t<blurb>%s</blurb>\n",
		encode_entities_numeric($notes))
		if (length $notes);

	my $yield = firstOfLiteral($S,
		['http://donnafales.com/2002/07/28/recipe-schema#x-yield'],
		['http://www.w3.org/1999/02/22-rdf-syntax-ns#value']);
	$rv .= sprintf("\t\t<blurb>%s</blurb>\n",
		encode_entities_numeric($notes))
		if (length $notes);

	my $preptime = firstOfLiteral($S,
		['http://donnafales.com/2002/07/28/recipe-schema#x-preparation-time'],
		['http://www.w3.org/1999/02/22-rdf-syntax-ns#value']);
	$rv .= sprintf("\t\t<preptime>%s</preptime>\n",
		encode_entities_numeric($preptime))
		if (length $preptime);

	my $source  = firstOf($S,
		'http://purl.org/dc/terms/source',
		'http://purl.org/dc/elements/1.1/source');
	$source = $S->{page}->uri unless ($source);
	my $stitle  = firstOfLiteral($triples->{$source},
		['http://purl.org/dc/terms/title',
		'http://purl.org/dc/elements/1.1/title'],
		['http://www.w3.org/1999/02/22-rdf-syntax-ns#value']);
	$stitle = $source unless (length $stitle);
	$rv .= sprintf("\t\t<source><hyperlink url=\"%s\">%s</hyperlink></source>\n",
		encode_entities_numeric($source),
		encode_entities_numeric($stitle))
		if (length $source);

	my @tags = allOfLiteral($S,
		['http://www.holygoat.co.uk/owl/redwood/0.1/tags/taggedWithTag'],
		['http://www.holygoat.co.uk/owl/redwood/0.1/tags/name',
		'http://www.w3.org/1999/02/22-rdf-syntax-ns#value']);
	foreach my $tag (@tags)
	{
		$rv .= sprintf("\t\t<genre>%s</genre>\n",
			encode_entities_numeric($tag));
	}
	
	$rv .= "\t</recipeinfo>\n";

	$rv .= "\t<ingredientlist>\n";
	my @ingredients = allOfObj($S,
		'http://donnafales.com/2002/07/28/recipe-schema#ingredient');
	foreach my $i (@ingredients)
	{
		if ($i->type)
		{
			my $I = findSubject($triples, $i->toString);
			if ($I)
			{
				my $label = firstOfLiteral($I,
					['http://www.w3.org/2000/01/rdf-schema#label'],
					['http://www.w3.org/1999/02/22-rdf-syntax-ns#value']);
				my $quantity = firstOfLiteral($I,
					['http://buzzword.org.uk/rdf/measure#quantity'],
					['http://www.w3.org/1999/02/22-rdf-syntax-ns#value']);
				my $unit = firstOfLiteral($I,
					['http://buzzword.org.uk/rdf/measure#quantity'],
					['http://buzzword.org.uk/rdf/measure-aux#unit']);
				my $item = firstOfLiteral($I,
					['http://www.w3.org/1999/02/22-rdf-syntax-ns#value'],
					['http://www.w3.org/1999/02/22-rdf-syntax-ns#value']);
				my @notes = allOfLiteral($I,
					['http://purl.org/dc/terms/abstract'],
					['http://www.w3.org/1999/02/22-rdf-syntax-ns#value']);

				if ($item && $quantity)
				{
					my $str;
					if ($unit && ($unit ne $quantity))
					{
						$str = sprintf("<quantity>%s</quantity> <unit>%s</unit> <fooditem>%s</fooditem>",
							encode_entities_numeric($quantity),
							encode_entities_numeric($unit),
							encode_entities_numeric($item));
					}
					else
					{
						$str = sprintf("<quantity>%s</quantity> <fooditem>%s</fooditem>",
							encode_entities_numeric($quantity),
							encode_entities_numeric($item));
					}
					foreach my $n (@notes)
					{
						$str .= sprintf(" (%s)", encode_entities_numeric($n));
					}
					$rv .= "\t\t<ingredient>$str</ingredient>\n";
					next;
				}
				elsif ($label)
				{
					$rv .= sprintf("\t\t<ingredient>%s</ingredient>\n",
						encode_entities_numeric($label));
					next;
				}
			}
		}
		$rv .= sprintf("\t\t<ingredient>%s</ingredient>\n",
			encode_entities_numeric($i->toString));
	}
	$rv .= "\t</ingredientlist>\n";

	my $directions = firstOfLiteral($S,
		['http://donnafales.com/2002/07/28/recipe-schema#directions'],
		['http://www.w3.org/1999/02/22-rdf-syntax-ns#value']);
	$directions =~ s/^1\. / 1. /gm;		
	$directions =~ s/^/\t\t/gm;
	$rv .= sprintf("\t<preparation>\n%s\n\t</preparation>\n",
		encode_entities_numeric($directions));

	return "$rv</recipe>\n\n";
}


1;
